﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ClientLib.MonitorState2;
using Bus = WpfDataBingding.TaskServiceReference;


namespace WpfDataBingding.PageCollection.MonitorPage
{
    /// <summary>
    /// DeviceStateChange.xaml 的交互逻辑
    /// </summary>
    public partial class DeviceStateChange : Page
    {
        private string dev_id;
        private string mon_id;

        public DeviceStateChange()
        {
            InitializeComponent();
        //    this.Device_state.DataContext = deivce;
        }

        public DeviceStateChange(DeviceDynamic device)
        {
            InitializeComponent();
            this.dev_id = device.Device_ID;
            this.mon_id = device.Monitor_ID;
            this.Device_state.DataContext = device;
            this.groupBoxDevice.Header = device.Device_ID;
        }
        private void Button_ClickSet(object sender, RoutedEventArgs e)
        {
            Button source = e.Source as Button;
            DockPanel txtblock = source.Parent as DockPanel;
            Label leb = (Label)txtblock.FindName("state_name");
            TextBox txtbox = (TextBox)txtblock.FindName("state_value");
            string state_name =(string)leb.Content;
            string state_value = (string)txtbox.Text;

            /*********************************************
          仅供下变频器设置频率,需要设计逻辑层
         *********************************************/
            double val = Double.Parse(state_value);
            Bus.AttributeValue staval=new Bus.AttributeValue();
            
            staval.Type="double";
            staval.Value=val;

            Collection devCollection=(Collection)Application.Current.Properties["MonitorCollection"];
            DeviceDynamic device=devCollection.GetDevice(dev_id,mon_id);

           // MessageBox.Show(string.Format("{0} 数据不合法!",state_value));

            Bus.DeviceState deviceState = new Bus.DeviceState();

            deviceState.AgentID = device.Monitor_ID;
            deviceState.DevID = device.Device_ID;
            deviceState.DevType = device.Dev_Type;
            deviceState.Name = state_name;
            deviceState.Value = staval;

            using (Bus.DataBusServiceClient client = new WpfDataBingding.TaskServiceReference.DataBusServiceClient())
            {
              bool issucess=client.SetStates(new Bus.DeviceState[]{deviceState});
              if (issucess == true)
              {
                  MessageBox.Show("属性设置成功！");
              }
              else
              {
                  MessageBox.Show("属性修改失败！");
              }
            }
            
        }

        private void buttonBack_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.GoBack();
        }

       
    }
}
